package com.tomhu.controler;

import com.tomhu.doservices.BracketServices;
import com.tomhu.util.ComparePriority;
import com.tomhu.util.Error;

public class BracketControler {

	public static void dealWithOperate(String[] strs) {

//		showResult(strs);
		// 对第一个进行的解析
		String[] strs0 = strs[0].split(" ");
		// 对第二个进行首先的解析
		String[] strs1 = strs[1].split(" ");
		// 对第三个进行首先的解析
		String[] strs2 = strs[2].split(" ");

		// 三个运算符！
		String operate1 = strs0[1];
		String operate2 = strs1[1];
		String operate3 = strs2[1];

		// 四个重要的数据结果！
		String strs04 = strs0[4];
		String strs10 = strs1[0];
		String strs12 = strs1[2];
		String strs14 = strs1[4];
		String strs20 = strs2[0];
		String strs22 = strs2[2];

		// 二个重要的替换表达式！
		String placeStr1 = strs[0].split(" = ")[0];
		String placeStr2 = strs[1].split(" = ")[0];
		String placeStr3 = strs[2].split(" = ")[0];
		
		//第一次的结果没有被第二次运算的使用！！
		/*
		 * 1. 减法和除法！
		 * 2. 优先级的问题！
		 * 3. 数字前后的问题。
		 */
		if (strs04.equals(strs10) || strs04.equals(strs12)) {
			if (ComparePriority.isLowPriority(operate1, operate2)) {
				placeStr1 = "(" + placeStr1 + ")";
			} else if (ComparePriority.isEqualPriority(operate1, operate2) && strs04.equals(strs12)) {
				if (operate2.equals("-") || operate2.equals("/")) {
					placeStr1 = BracketServices.replaceStrs(operate1, placeStr1);
				} 
			}
			strs[1] = strs[1].replaceFirst(strs04, placeStr1);
			placeStr2 = strs[1].split(" = ")[0];
			
			if (ComparePriority.isLowPriority(operate2, operate3)) {
				placeStr2 = "(" + placeStr2 + ")";
			} else if (ComparePriority.isEqualPriority(operate2, operate3) && strs14.equals(strs22)) {
				if (operate3.equals("-") || operate3.equals("/")) {
					placeStr2 = BracketServices.replaceStrs(operate2, placeStr2);
					if (ComparePriority.isEqualPriority(operate1, operate3)) {
						placeStr1 = BracketServices.replaceStrs(operate1, placeStr1);
					}
				} 
			}
			strs[2] = strs[2].replaceFirst(strs14, placeStr2);
			
		} 
		//第一次的结果被第二次的使用！！
		else {
			if (ComparePriority.isLowPriority(operate1, operate3)) {
				placeStr1 = "(" + placeStr1 + ")";
			} else if (ComparePriority.isEqualPriority(operate1, operate3) && strs04.equals(strs22)) {
				if (operate3.equals("-") || operate3.equals("/")) {
					placeStr1 = BracketServices.replaceStrs(operate1, placeStr1);
				} 
			}
//			strs[2] = strs[2].replaceFirst(strs04, placeStr1);
//			placeStr3 = strs[2].split(" = ")[0];
			
			if (ComparePriority.isLowPriority(operate2, operate3)) {
				placeStr2 = "(" + placeStr2 + ")";
			} else if (ComparePriority.isEqualPriority(operate2, operate3) && strs14.equals(strs22)) {
				if (operate3.equals("-") || operate3.equals("/")) {
					placeStr2 = BracketServices.replaceStrs(operate2, placeStr2);
				} 
			}
			strs[2] = strs[2].replaceFirst(strs14, placeStr2);
			
			strs[2] = strs[2].replaceFirst(strs04, placeStr1);
			placeStr3 = strs[2].split(" = ")[0];
		}
		placeStr3 = strs[2].split(" = ")[0];
		System.out.println(placeStr3.replaceAll(".0", "") + " = 24");
	}
	
	public static void showResult(String[] strs) {
		
		if (strs == null) {
			return ;
		}
		for(String str: strs) {
			System.out.println(str);
		}
	}
}
